class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size();
        int min_len = 100010;
        if (n == 1)
        {
            if (nums[0] >= target)
                return 1;
            else
                return 0;
        }

        int l = 0, r = 0;
        int sum = 0;
        for (l = 0, r = 0; r < nums.size();)
        {
            sum += nums[r];
            while (sum >= target)
            {
                min_len = min(min_len, r - l + 1);
                sum -= nums[l++];
            }
            r++;
        }

        if (l == 0)
            return 0;
        else
            return min_len;

    }
};